/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer;

import com.vividsolutions.jts.geom.Geometry;

import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import nl.cloudfarming.client.model.Bean;
import nl.cloudfarming.client.model.BeanNode;
import nl.cloudfarming.client.model.DataProvider;
import nl.cloudfarming.client.model.Surface;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.lookup.Lookups;

/**
 * Node representing an object represented on a map layer
 *
 *
 * @param <G> Geometry type - Geometry type wrapped in the LayerObject
 * @param <B> Bean - Bean type wrapped in the LayerObject
 * @param <L> Layer type to use with this LayerObjectNode
 * @author Timon Veenstra
 */
public class LayerObjectNode<G extends Geometry, B extends Bean, L extends LayerObject<G, B>> extends BeanNode<B> {

    private static final String ZOOM_ACTION_NAME = NbBundle.getMessage(LayerObjectNode.class, "layer_object.zoom_to_action");
    private static final String EDIT_ACTION_NAME = NbBundle.getMessage(LayerObjectNode.class, "layer_object.edit_action");
    private final L layerObject;
    private final Layer layer;

    public LayerObjectNode(Layer layer, L layerObject, DataProvider dataProvider) throws IntrospectionException {
        super(layerObject.getObject(), Children.LEAF, Lookups.fixed(layer, layerObject,layerObject.getObject()), dataProvider);
        this.layerObject = layerObject;
        this.layer = layer;
    }

    public L getLayerObject() {
        return this.layerObject;
    }

    /**
     * Returns the HTML display name based on the visibility of the layer.
     *
     * @return The HTML display name of the node (layer)
     */
    @Override
    public String getHtmlDisplayName() {
        String displayName = layerObject.getTooltipText();

        return "<font color='#000000'>" + displayName + "</font>";
    }

    @Override
    public Action[] getActions(boolean context) {
        List<Action> actions = new ArrayList();
        actions.addAll(Arrays.asList(super.getActions(context)));
        actions.add(new ZoomToAction());
        actions.add(new EditAction());
        return LayerObject.Actions.mergeActions(layerObject, actions.toArray(new Action[]{}));
    }

    @Override
    public Action getPreferredAction() {
        return new ZoomToAction();
    }

    public class EditAction extends LayerObjectAction {

        public EditAction() {
            super(EDIT_ACTION_NAME, null);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (layerObject.getObject() instanceof Surface) {
                SurfaceEditor editor = Lookup.getDefault().lookup(SurfaceEditor.class);
                if (editor != null) {
                    editor.edit((Surface)layerObject.getObject());
                }
            }
        }
    }

    /**
     * Zoom the map in to the layer object
     */
    public class ZoomToAction extends LayerObjectAction {

        public ZoomToAction() {
            super(ZOOM_ACTION_NAME, null);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            MapController controller = MapController.Registry.getLookup().lookup(MapController.class);
            controller.zoomToObject(getLayerObject(), layer);
        }
    }
}
